global root "C:\Users\hanwa\Desktop\replication"

use "$root\data\IMC_HA_Quarterly_Panel_clean.dta", replace

***Figure 5A-Debt identified
preserve
drop if startdate==-1
keep if cem_matched==1

g year_month=ym(year,month)
g bens_year=int(startdate/100)
g bens_month=startdate-(bens_year*100)
g time_bens_intro=ym(bens_year,bens_month)

gen bench_time_mon = year_month-time_bens_intro
gen bench_time_mon_0=bench_time_mon==0
foreach num of numlist 1(1)22{
gen bench_time_mon_p`num'= bench_time_mon == `num' 
}

foreach num of numlist 1(1)14{
gen bench_time_mon_m`num'= bench_time_mon == -`num'
}

gen bench_time_mon_m15= bench_time_mon <= -15 & bench_time_mon!=.
gen bench_time_mon_p23= bench_time_mon >= 23 & bench_time_mon!=.

drop bench_time_mon

global prepost bench_time_mon_m12 bench_time_mon_m9 bench_time_mon_m6 bench_time_mon_m3  zero bench_time_mon_0 bench_time_mon_p3 bench_time_mon_p6 bench_time_mon_p9 bench_time_mon_p12 bench_time_mon_p15 bench_time_mon_p18 bench_time_mon_p21
gen zero = 0 


est clear
stackedev identir $prepost, cohort(startdate) time(region_yq) never_treat(no_treat) unit_fe(code) clust_unit(code) covariates (deficit_rate grant_dependence lnpop lnclaim lnwage  kids private umemploy pension vulnerable )
eststo f1

graph drop _all
coefplot f1 ///
,omitted keep(bench_time_mon_m12 bench_time_mon_m9 bench_time_mon_m6 bench_time_mon_m3  zero bench_time_mon_0 bench_time_mon_p3 bench_time_mon_p6 bench_time_mon_p9 bench_time_mon_p12 bench_time_mon_p15 bench_time_mon_p18 bench_time_mon_p21)  /// 
vertical lcolor(gs6) mlcolor(gs6) mfcolor(gs6)  msize (*1.6) msymbol(Oh)  ///
legend(off) ///
scheme(s1mono) ///
graphregion(margin(5 9 5 5)) ///
xlabel( 1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5" 12 "6" 13 "7") ///
ytitle("Debt identified", margin(0 1 4 0)) ///
xtitle("Season") ///
yline(0, lcolor(red) lwidth(*1.1)) ///
xline(6, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
xline(10, lwidth(medium) lpattern(dash) lcolor(cranberry))
graph export "$root\figures\figure5A.png", replace
restore

***Figure 5B-Debt recovered
preserve
drop if startdate==-1
keep if cem_matched==1

g year_month=ym(year,month)
g bens_year=int(startdate/100)
g bens_month=startdate-(bens_year*100)
g time_bens_intro=ym(bens_year,bens_month)

gen bench_time_mon = year_month-time_bens_intro
gen bench_time_mon_0=bench_time_mon==0
foreach num of numlist 1(1)22{
gen bench_time_mon_p`num'= bench_time_mon == `num' 
}

foreach num of numlist 1(1)14{
gen bench_time_mon_m`num'= bench_time_mon == -`num'
}

gen bench_time_mon_m15= bench_time_mon <= -15 & bench_time_mon!=.
gen bench_time_mon_p23= bench_time_mon >= 23 & bench_time_mon!=.

drop bench_time_mon

global prepost bench_time_mon_m12 bench_time_mon_m9 bench_time_mon_m6 bench_time_mon_m3  zero bench_time_mon_0 bench_time_mon_p3 bench_time_mon_p6 bench_time_mon_p9 bench_time_mon_p12 bench_time_mon_p15 bench_time_mon_p18 bench_time_mon_p21
gen zero = 0 


est clear
stackedev recover $prepost, cohort(startdate) time(region_yq) never_treat(no_treat) unit_fe(code) clust_unit(code) covariates (deficit_rate grant_dependence lnpop lnclaim lnwage  kids private umemploy pension vulnerable )
eststo f1

graph drop _all
coefplot f1 ///
,omitted keep(bench_time_mon_m12 bench_time_mon_m9 bench_time_mon_m6 bench_time_mon_m3  zero bench_time_mon_0 bench_time_mon_p3 bench_time_mon_p6 bench_time_mon_p9 bench_time_mon_p12 bench_time_mon_p15 bench_time_mon_p18 bench_time_mon_p21)  /// 
vertical lcolor(gs6) mlcolor(gs6) mfcolor(gs6)  msize (*1.6) msymbol(Oh)  ///
legend(off) ///
scheme(s1mono) ///
graphregion(margin(5 9 5 5)) ///
xlabel( 1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5" 12 "6" 13 "7") ///
ytitle("Debt recovered", margin(0 1 4 0)) ///
xtitle("Season") ///
yline(0, lcolor(red) lwidth(*1.1)) ///
xline(6, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
xline(10, lwidth(medium) lpattern(dash) lcolor(cranberry))
graph export "$root\figures\figure5B.png", replace
restore

***Figure 5C-Debt written off
preserve
drop if startdate==-1
keep if cem_matched==1

g year_month=ym(year,month)
g bens_year=int(startdate/100)
g bens_month=startdate-(bens_year*100)
g time_bens_intro=ym(bens_year,bens_month)

gen bench_time_mon = year_month-time_bens_intro
gen bench_time_mon_0=bench_time_mon==0
foreach num of numlist 1(1)22{
gen bench_time_mon_p`num'= bench_time_mon == `num' 
}

foreach num of numlist 1(1)14{
gen bench_time_mon_m`num'= bench_time_mon == -`num'
}

gen bench_time_mon_m15= bench_time_mon <= -15 & bench_time_mon!=.
gen bench_time_mon_p23= bench_time_mon >= 23 & bench_time_mon!=.

drop bench_time_mon

global prepost bench_time_mon_m12 bench_time_mon_m9 bench_time_mon_m6 bench_time_mon_m3  zero bench_time_mon_0 bench_time_mon_p3 bench_time_mon_p6 bench_time_mon_p9 bench_time_mon_p12 bench_time_mon_p15 bench_time_mon_p18 bench_time_mon_p21
gen zero = 0 


est clear
stackedev writter $prepost, cohort(startdate) time(region_yq) never_treat(no_treat) unit_fe(code) clust_unit(code) covariates (deficit_rate grant_dependence lnpop lnclaim lnwage  kids private umemploy pension vulnerable )
eststo f1

graph drop _all
coefplot f1 ///
,omitted keep(bench_time_mon_m12 bench_time_mon_m9 bench_time_mon_m6 bench_time_mon_m3  zero bench_time_mon_0 bench_time_mon_p3 bench_time_mon_p6 bench_time_mon_p9 bench_time_mon_p12 bench_time_mon_p15 bench_time_mon_p18 bench_time_mon_p21)  /// 
vertical lcolor(gs6) mlcolor(gs6) mfcolor(gs6)  msize (*1.6) msymbol(Oh)  ///
legend(off) ///
scheme(s1mono) ///
graphregion(margin(5 9 5 5)) ///
xlabel( 1 "-5" 2 "-4" 3 "-3" 4 "-2" 5 "-1" 6 "0" 7 "1" 8 "2" 9 "3" 10 "4" 11 "5" 12 "6" 13 "7") ///
ytitle("Debt written off", margin(0 1 4 0)) ///
xtitle("Season") ///
yline(0, lcolor(red) lwidth(*1.1)) ///
xline(6, lwidth(medium) lpattern(dash) lcolor(cranberry)) ///
xline(10, lwidth(medium) lpattern(dash) lcolor(cranberry))
graph export "$root\figures\figure5C.png", replace
restore

